<template>
  <view class="page">
    <view class="tips" v-if="userList.length==0">已显示全部</view>
    <scroll-view scroll-y="true" style="height: 100%">
      <template v-if="userList.length > 0">
        <block v-for="(item, index) in userList" :key="index">
          <usercard :item="item" :index="index" />
        </block>
      </template>
    </scroll-view>
  </view>
</template>

<script>
import usercard from "./components/user-card/user-card";
import api from '@/api/index.js'
export default {
  components: {
    usercard,
  },
  data() {
    return {
      userList: [
        {
          friend_user: {
            _id: '2',
            nickname: '2503任金祥大王',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '明天开会吗？',
            create_time: 1760908621564
          },
          unread_count: 9
        },
        {
          friend_user: {
            _id: '3',
            nickname: '111',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '收到文件了吗？',
            create_time: 1760868988316
          },
          unread_count: 1
        },
        {
          friend_user: {
            _id: '4',
            nickname: '索大最俊朗',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '谢谢你的帮助！',
            create_time: 1760858098792
          },
          unread_count: 5
        },
        {
          friend_user: {
            _id: '5',
            nickname: '张恒阳',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '你好！',
            create_time: 1760878613216
          },
          unread_count: 1
        },
        {
          friend_user: {
            _id: '6',
            nickname: '张恒阳的组长',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '最近怎么样？',
            create_time: 1760939022804
          },
          unread_count: 6
        },
        {
          friend_user: {
            _id: '7',
            nickname: '张恒阳的总监',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '项目进展如何？',
            create_time: 1760939568032
          },
          unread_count: 5
        },
        {
          friend_user: {
            _id: '8',
            nickname: '无敌大香蕉',
            avatar: '/static/images/default-avatar.png'
          },
          chatDetail: {
            content: '明天开会吗？',
            create_time: 1760897114282
          },
          unread_count: 9
        }
      ],
    };
  },

  onLoad() {
    this.getUserList()
  },
  onShow(){
    // 使用模拟数据，注释掉网络请求
    // this.getMeaasge()
  },

  methods: {
    //发送请求获得消息列表
    getUserList() {
      this.$api.chat.getChatList().then((res) => {
        if(res.code === 200){
          // 转换后端数据格式为前端需要的格式
          this.userList = res.data.map(item => ({
            friend_user: {
              _id: item._id,
              nickname: item.nickname,
              avatar: '/static/images/default-avatar.png' // 使用默认头像
            },
            chatDetail: {
              content: item.last_message,
              create_time: item.last_time
            },
            unread_count: item.unread_count
          }));
        } else {
          uni.showToast({
            icon:'none',
            title: res.message || '获取消息列表失败',
            duration: 2000
          })
        }
      }).catch(err => {
        console.error('获取消息列表失败:', err)
        uni.showToast({
          icon:'none',
          title:'获取消息列表失败',
        })
      })
    },

    // 使用模拟数据，注释掉网络请




    getMeaasge(){
    }
  },
};
</script>

<style>
page {
  background-color: #f5f6f7;
}
</style>